package fr.upmc.cie.ext;

import java.util.ArrayList;

import fr.upmc.cie.R;
import fr.upmc.cie.data.CategorieItem;
import fr.upmc.cie.data.ChannelItem;
import fr.upmc.cie.data.TicketItem;
import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MainPageExpandableListAdapter extends BaseExpandableListAdapter{
	
	private Context context;
	private ArrayList<ArrayList<? extends Object>> children;
	private ArrayList<String> al;
	
	public MainPageExpandableListAdapter(Context context, 
				ArrayList<TicketItem> recent, 
				ArrayList<CategorieItem> categories,
				ArrayList<ChannelItem> channels){
		
		this.context = context;
		children = new ArrayList<ArrayList<? extends Object>>();
		children.add(recent);
		children.add(categories);
		children.add(channels);
		al = new ArrayList<String>();
		al.add(context.getString(R.string.portail_recent));
		al.add(context.getString(R.string.portail_categories));
		al.add("Channels");
	}

	@Override
	public int getGroupCount() {
		return children.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return children.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return children.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		int count = 0;
		for (int i=0; i<groupPosition; i++)
			count += getChildrenCount(groupPosition);
		return count+childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView view = getGenericView(40,36);
		view.setTypeface(null, Typeface.BOLD_ITALIC);
		view.setText(al.get(groupPosition));
		return view;

	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		TextView view = getGenericView(64,18);
		view.setText(getChild(groupPosition,childPosition).toString());
		if (groupPosition==1||groupPosition==2) view.setTypeface(Typeface.DEFAULT_BOLD);
		return view;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

    public TextView getGenericView(int height, int leftpadding) {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, height);
        TextView textView = new TextView(context);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(leftpadding, 0, 0, 0);
        return textView;
    }
    
    public void notifyDataSetChanged(){
    	super.notifyDataSetChanged();
    }
}
